package com.zkthink.ceres.finance.controller;


import com.zkthink.base.R;
import com.zkthink.ceres.common.utils.DesensitizeUtil;
import com.zkthink.ceres.finance.dto.BankDTO;
import com.zkthink.ceres.finance.dto.MemberCollectingAccountSaveDTO;
import com.zkthink.ceres.finance.dto.MemberCollectingAccountUpdateDTO;
import com.zkthink.ceres.finance.entity.MemberCollectingAccount;
import com.zkthink.ceres.finance.service.MemberCollectingAccountService;
import com.zkthink.ceres.sms.dto.VerificationCodeDTO;
import com.zkthink.log.annotation.SysLog;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 前端控制器
 * 收款帐户表
 * </p>
 *
 * @author vivi.huang
 * @date 2020-05-25
 */
@Slf4j
@Validated
@RestController
@RequestMapping("/memberCollectingAccountController")
@Api(value = "MemberCollectingAccountController", tags = "客户收款帐户表")
//@PreAuth(replace = "collectingAccount:")
@AllArgsConstructor
public class MemberCollectingAccountController {

    @Autowired
    private MemberCollectingAccountService collectingAccountService;


    /**
     * 绑定收款帐号
     */
    @ApiOperation(value = "查询收款帐号")
    @GetMapping("/getCollectingAccount/{mid}")
    public R<MemberCollectingAccount> getCollectingAccount(@PathVariable("mid") Long mid) {
        MemberCollectingAccount collectingAccount = collectingAccountService.selectByMid(mid).getData();

        if (collectingAccount == null) {
            return R.success(null);
        }

        collectingAccount.setCardNumber(DesensitizeUtil.desensitize(collectingAccount.getCardNumber()));
        collectingAccount.setCardholderName(DesensitizeUtil.desensitize(collectingAccount.getCardholderName()));
        return R.success(collectingAccount);
    }

    /**
     * 绑定收款帐号
     */
    @ApiOperation(value = "绑定收款帐号")
    @PostMapping("/binding")
    @SysLog(value = "绑定收款帐号", request = false)
    public R<Boolean> bindingAccount(@RequestBody @Validated MemberCollectingAccountSaveDTO collectingAccountSaveDTO) {
        return collectingAccountService.bindingMemberAccount(collectingAccountSaveDTO);
    }

    /**
     * 更换收款帐号
     */
    @ApiOperation(value = "更换收款帐号")
    @PostMapping("/change")
    @SysLog(value = "更换收款帐号", request = false)
    public R<Boolean> changeAccount(@RequestBody @Validated MemberCollectingAccountUpdateDTO collectingAccountUpdateDTO) {
        return collectingAccountService.changeMemberAccount(collectingAccountUpdateDTO);
    }

    /**
     * 解绑收款帐号
     */
    @ApiOperation(value = "解绑收款帐号")
    @PostMapping("/cancel")
    @SysLog(value = "解绑收款帐号", request = false)
    public R<Boolean> cancelBindingAccount(@RequestBody @Validated VerificationCodeDTO verificationCodeDTO) {
        return collectingAccountService.cancelBindingMemberAccount(verificationCodeDTO);
    }

    @ApiOperation(value = "银行列表")
    @GetMapping("/bankList")
    @SysLog(value = "解绑收款帐号", request = false)
    //@PreAuth("hasPermit('{}update')")
    public R<List<BankDTO>> findBankList() {
        return collectingAccountService.findBankList();
    }
}
